import {observe} from "./observe";
import Dep from "./Dep";
export default function defineReactive(data,key,val){
  const dep = new Dep()
  if (arguments.length == 2) {
    val = data[key]
  }
  // 形成递归，多个函数互相调用
  let childOb = observe(val)
  Object.defineProperty(data,key,{
    enumerable:true,
    configurable:true,
    get(){
      console.log(`访问${key}属性`);
      if (Dep.target){
        dep.depend();
        if (childOb){
          childOb.dep.depend()
        }
      }
      return val
    },
    set(newValue){
      console.log(`试图修改${key}属性`);
      if (val === newValue) return
      val = newValue;
      childOb = observe(newValue)
      // 发布订阅模式，通知
      dep.notify()
    }
  })
}